package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import android.util.Log
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.stars.empoly.BR
import com.stars.empoly.activity.PayActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.model.*
import com.stars.empoly.model.bean.ChoseCityDatas
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

class IssueVModel(application: Application) : BViewModel<BRepository>(application) {


    var item: MutableLiveData<ChoseCityDatas> = MutableLiveData()

    //用车时间
    var start_driving_time: String? = ""
    var end_driving_time: String? = ""
    var timeOne: MutableLiveData<String> = MutableLiveData()

    //卡车类型
    var isScooter = false
    var type_id: String? = ""
    var typeIdName = MutableLiveData<String>()//选择的卡车名
    var carTypeList = MutableLiveData<List<CarCategory>>()
    //车辆状态

    var vehicle_status: String? = "0"
    var vehicletatusName = MutableLiveData<String>("正常")

    //付款方式
    var payment: String? = "0"//0全款,1=到付,2=付定金 默认全款

    var choseOne = MutableLiveData<Boolean>(true)
    var choseTwo = MutableLiveData<Boolean>(false)
    var choseThree = MutableLiveData<Boolean>(false)
    //总金额+定金

    var calcu = MutableLiveData<Calculation>()
    //下完单返回的参数

    var placeOrder = MutableLiveData<OrderAdd>()

    //付款金额--暂时没有用
    var amount = MutableLiveData<String>()

    //定金金额
    var deposit:String?=null

    //再来一单的数据
    var bean= MutableLiveData<CompreDetailsBean>()



    @Bindable
    var money: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.money)
        }


    @Bindable
    var vehicleModel: String? = null
        //车辆型号
        set(value) {
            field = value
            notifyPropertyChanged(BR.vehicleModel)
        }


    @Bindable
    var consignorName: String? = null
        //发车人姓名
        set(value) {
            field = value
            notifyPropertyChanged(BR.consignorName)
        }


    @Bindable
    var phone: String? = null
        //发车人电话
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }


    @Bindable
    var receivingName: String? = null
        //发车人姓名
        set(value) {
            field = value
            notifyPropertyChanged(BR.receivingName)
        }


    @Bindable
    var mobile: String? = null
        //发车人电话
        set(value) {
            field = value
            notifyPropertyChanged(BR.mobile)
        }


    @Bindable
    var remarks: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.remarks)
        }


    fun toChoseType(type: Int) {

        when (type) {
            1 -> {
                payment = "0"
                choseOne.value = true
                choseTwo.value = false
                choseThree.value = false
            }
            2 -> {
                payment = "1"
                choseOne.value = false
                choseTwo.value = true
                choseThree.value = false
            }
            3 -> {
                payment = "2"
                choseOne.value = false
                choseTwo.value = false
                choseThree.value = true
            }
        }

    }

    fun toChangePlace() {
        //切换 起始地点
        var ss = ChoseCityDatas(
                item.value?.endPosition, item.value?.endDetailsPosition,
                item.value?.endCity, item.value?.endLat, item.value?.endLng,
                item.value?.startPosition, item.value?.startDetailsPosition,
                item.value?.startCity, item.value?.startLat, item.value?.startLng,
        )

        item.value = ss

    }

    fun getCarType() {


    }


    fun setSumFee() {

        viewModelScope.launch {

            var it = repository().lreq { NetEngine.service.calculation(item.value?.startLat.toString(), item.value?.startLng.toString(), item.value?.endLat.toString(), item.value?.endLng.toString(), type_id) }
            if (it.ok()) {
                calcu.value = it.data
                amount.value = it.data?.amount
                money = it.data?.amount
                deposit=it.data?.deposit
            }


        }
    }


    fun toPushSource() {
        //发布货源

        if (TextUtils.isEmpty(start_driving_time)) {

            toast("请选择用车时间")
            return
        }

        if (TextUtils.isEmpty(vehicleModel)) {

            toast("请输入车辆型号")
            return
        }
        if (TextUtils.isEmpty(consignorName)) {

            toast("输入发车人真实姓名")
            return
        }
        if (TextUtils.isEmpty(phone)) {

            toast("输入发车人电话号码")
            return
        }


        if (TextUtils.isEmpty(receivingName)) {

            toast("输入接车人真实姓名")
            return
        }
        if (TextUtils.isEmpty(mobile)) {

            toast("输入接车人电话号码")
            return
        }
        if (TextUtils.isEmpty(type_id)) {

            toast("请选择板车类型")
            return
        }
        //var  payment:String?="0"//0全款,1=到付,2=付定金 默认全款

        if (payment.equals("0") || payment.equals("2")) {

            if (TextUtils.isEmpty(money)) {
                toast("请输入合计金额")
                return
            }
        }


//        Log.e("-shy-", "vehicle_model= "+vehicleModel )
//        Log.e("-shy-", "vehicle_status= "+vehicle_status )
//        Log.e("-shy-", "start_city_name= "+item.value?.startCity )
//        Log.e("-shy-", "start_lat= "+ item.value?.startLat.toString() )
//        Log.e("-shy-", "start_lat= "+item.value?.startLng.toString() )
//        Log.e("-shy-", "end_city_name= "+ item.value?.endCity )
//        Log.e("-shy-", "end_lat= "+ item.value?.endLat.toString() )
//        Log.e("-shy-", "end_lng= "+ item.value?.endLng.toString() )
//        Log.e("-shy-", "payment= "+payment )
//        Log.e("-shy-", "remarks= "+remarks )
//        Log.e("-shy-", "start_driving_time= "+start_driving_time )
//        Log.e("-shy-", "end_driving_time= "+ end_driving_time)
//        Log.e("-shy-", "type_id= "+type_id )
//        Log.e("-shy-", "consignor_name= "+consignorName )
//        Log.e("-shy-", "consignor_mobile= "+ phone )
//        Log.e("-shy-", "start_address= "+  item.value?.startDetailsPosition)
//        Log.e("-shy-", "start_position= "+  item.value?.startPosition, )
//        Log.e("-shy-", "end_address= "+  item.value?.endDetailsPosition)
//        Log.e("-shy-", "end_position= "+ item.value?.endPosition, )
//        Log.e("-shy-", "receivingName= "+  receivingName)
//        Log.e("-shy-", "mobile= "+ mobile )

        if (payment.equals("1")) {//到付

            viewModelScope.launch {

                var it = repository().lreq {
                    NetEngine.service.orderAdd(vehicleModel, vehicle_status,

                            item.value?.startCity, item.value?.startLat.toString(), item.value?.startLng.toString(),
                            item.value?.endCity, item.value?.endLat.toString(), item.value?.startLng?.toString(),
                            item.value?.startDetailsPosition, item.value?.startPosition,
                            item.value?.endDetailsPosition, item.value?.endPosition,
                            payment, remarks, start_driving_time, end_driving_time, type_id,
                            consignorName, phone, money, receivingName, mobile)
                }

                if (it.ok(true)) {

                    placeOrder.value = it.data
                }


            }
        } else {//去支付界面

            var bean = PlaceOrder(vehicleModel, vehicle_status,
                    item.value?.startCity, item.value?.startLat.toString(), item.value?.startLng.toString(),
                    item.value?.endCity, item.value?.endLat.toString(), item.value?.startLng?.toString(),
                    item.value?.startDetailsPosition, item.value?.startPosition,
                    item.value?.endDetailsPosition, item.value?.endPosition,
                    payment, remarks, start_driving_time, end_driving_time, type_id,
                    consignorName, phone, money, receivingName, mobile)


            if(payment.equals("0")){
                //全款

                UiSwitch.bundleRes(repository().context as Activity, PayActivity::class.java,
                        B().putString("bean", Gson().toJson(bean))
                                .putString("price",money)
                                .ok(), MIntentAction.REQUEST_CODE_TWO)

                return
            }
            if(payment.equals("2")) {
                //付定金
                UiSwitch.bundleRes(repository().context as Activity, PayActivity::class.java,
                        B().putString("bean", Gson().toJson(bean))
                                .putString("price",deposit)
                                .ok(), MIntentAction.REQUEST_CODE_TWO)
            }




        }


    }

    fun getInfo(order_id:String?) {
        //订单详情

        viewModelScope.launch {
            var it = repository().preq { NetEngine.service.orderDetail(order_id) }

            if (it.ok()) {
                bean.value=it.data
            }

        }

    }



}
